<?php
namespace app\user\controller;

use think\Controller;
use think\Db;

class Chat extends Controller
{
    public function kefu(){
        $user=session('user');
        if(empty($user)){
            $this->error('您未登录,不能使用在线客服');
        }else{
            $this->assign([
                'formid'=>$user['id'],
                'toid'=>8
            ]);
            return view();
        }

    }


    //把数据保存到数据库
    public function savechat(){
        if (request()->isAjax()){
            $data=input('post.');
            $data['is_read']=0;
            db('chat')->insert(['formid'=>$data['formid'],'toid'=>$data['toid'],'content'=>$data['content'],'is_read'=>$data['is_read'],'date'=>$data['date']]);
        }
    }

    //把内容变为已读
    public function updateisread(){
        if (request()->isAjax()){
            $data=input('post.');
            db('chat')->where(['formid'=>$data['toid'],'toid'=>$data['formid']])->update(['is_read'=>1]);
        }
    }

    //获取历史数据
    public function gethistorymess(){
        $data=input('post.');

        $result = Db::query("select * from shop_chat where (formid=".$data['formid']." and toid=".$data['toid'].") or (formid=".$data['toid']." and toid=".$data['formid'].")   order by id");
        return $result;

    }


    public function changeNorRead(){
        $formid=input('toid');
        $toid=input('formid');
        db('chat')->where(['formid'=>$formid,'toid'=>$toid])->update(["is_read"=>1]);
    }
}